package org.ws.edu.web.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ws.edu.common.model.Appointment;
import org.ws.edu.dao.AppointmentDao;

@Controller
@RequestMapping(value = "/appointment", headers = "Accept=application/json")
public class AppointmentController {

    @Autowired
    private AppointmentDao appointmentDao;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Appointment> getAppointments() {
//        Appointment app1 = new Appointment();
//        app1.setName("pizza");
//        app1.setDescription("some food");
//        app1.setRating(12.5);
//        app1.setShortDescription("some");
//
//        Appointment app2 = new Appointment();
//        app2.setName("taxi");
//        app2.setDescription("car");
//        app2.setRating(12.5);
//        app2.setShortDescription("some");
//
//        appointmentDao.create(app2);
//        appointmentDao.create(app1);

        return appointmentDao.getList();

    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Appointment getAppointment(@PathVariable("id") long id) {
        return appointmentDao.get(id);

    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @Transactional(rollbackFor = Exception.class)
    public void deleteAppointment(@RequestBody Appointment appointment) {
        appointmentDao.delete(appointment);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @Transactional(rollbackFor = Exception.class)
    public void deleteAppointment(@PathVariable("id") long id) {
        appointmentDao.delete(id);
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @Transactional(rollbackFor = Exception.class)
    public void updateAppointment(@RequestBody Appointment appointment) {
        appointmentDao.update(appointment);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public void saveAppointment(@RequestBody Appointment appointment) {
        appointmentDao.create(appointment);
    }
}
